#include <windows.h>
#include <mmsystem.h>
#include "../Common/gvar.h"
#include "AudioCaptureWindow.h"


//---------------------- extern globals
extern HINSTANCE hInst;

//---------------------- internal globals
static	char			szWndClass[]		= "StreamboxAudioCapture";
static	HWND			hMainWnd			= 0;
static	UINT			TimerID				= 0;
static	DWORD			SysTime				= 0;
static	DWORD			TIMER_STEP			= 1000; // 1 second

BOOL bHasCallBack = FALSE;
AC_ONTIMERFUNC *lpCallback;

UINT  AcHandlerMsg;
BOOL bHasACHandler = FALSE;
AC_HANDLER *lpHandler = NULL;

////////////////////////////////////////////////////////////////////////////////
// Main Window Procedure
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

VOID CALLBACK MyTimerProc(
  HWND hwnd,         // handle to window
  UINT uMsg,         // WM_TIMER message
  UINT_PTR idEvent,  // timer identifier
  DWORD dwTime       // current system time
);

 
HWND AudioCaptureWindow_Create(int nWidth, int nHeigth, HWND hParentWnd)
{
	HINSTANCE mh = GetModuleHandle(NULL);
	WNDCLASS	wc;
	ATOM aWndClass = 0;
	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
	wc.lpfnWndProc		= (WNDPROC)MainWndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= mh;
	wc.hIcon			= 0;
	wc.hCursor			= 0;
	wc.hbrBackground	= 0;
	wc.lpszMenuName		= NULL;
    wc.lpszClassName	= (char*)&szWndClass;
	
	// register window class
	aWndClass		= RegisterClass(&wc);
	if (aWndClass == 0)	
	{
		return NULL;
	}

	if(hParentWnd == 0)
	{
		// create window
		hMainWnd = CreateWindow(szWndClass,"Streambox Everywhere Live Preview  Audio Only",
						WS_OVERLAPPED | WS_VISIBLE,
						0, 0, nWidth, nHeigth,
						NULL, NULL, hInst, NULL);
	} else
	{
		// create window
		hMainWnd = CreateWindow(szWndClass, "Streambox Everywhere Live Preview  Audio Only",
						WS_CHILD | WS_VISIBLE,
						0, 0, nWidth, nHeigth,
						hParentWnd, (HMENU) g_cs.nID, hInst, NULL);
	}
 	if(!hMainWnd)
	{
		DWORD dwErr = GetLastError();
		
		return NULL;
	}

	ShowWindow(hMainWnd, SW_SHOW);
	UpdateWindow(hMainWnd);

	return hMainWnd;
}

void AudioCaptureWindow_Destroy()
{
	UnregisterClass(szWndClass, hInst);
}



////////////////////////////////////////////////////////////////////////////////
// Main Window Procedure
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lRes = 0;
	hMainWnd	 = hWnd;
	switch (Msg)
	  {
		case (WM_CREATE):
		  {
			// setup timer
			if(hMainWnd)
			{
				SetTimer(hMainWnd, TimerID, TIMER_STEP, MyTimerProc);
			}
			lRes = DefWindowProc(hWnd, Msg, wParam, lParam);
		  } break;
		case (WM_CLOSE):
		case (WM_DESTROY):	
		  { 
			  KillTimer(hMainWnd, TimerID);
			PostQuitMessage(0); 
		  } break;
		default:
		{
			if(bHasACHandler && lpHandler)
			{
				if(Msg == AcHandlerMsg)
				{
					(*lpHandler)(wParam, lParam);
					break;
				}
			}
			lRes = DefWindowProc(hWnd, Msg, wParam, lParam);
		}
	  }
	return lRes;
}


////////////////////////////////////////////////////////////////////////////////
// Timer Callback
VOID CALLBACK MyTimerProc(
  HWND hwnd,         // handle to window
  UINT uMsg,         // WM_TIMER message
  UINT_PTR idEvent,  // timer identifier
  DWORD dwTime       // current system time
)
{
	if(!hMainWnd)
	{
		return;
	}

	if(bHasCallBack)
	{
		(*lpCallback)();
		KillTimer(hWndC, idEvent); // !!! test
	}

}

BOOL AudioCaptureWindow_SetTimerCallback(int set, AC_ONTIMERFUNC *func)
{
	if(func != NULL)
	{
		lpCallback = func;
		bHasCallBack = TRUE;
	} else
	{
		lpCallback = NULL;
		bHasCallBack = FALSE;
	}

	return TRUE;
}

void AudioCaptureWindow_AddHandler(UINT uMsg, AC_HANDLER *pFunc)
{
	if(pFunc != NULL)
	{
		bHasACHandler = TRUE;
		lpHandler = pFunc;
		AcHandlerMsg = uMsg;
	} else
	{
		bHasACHandler = FALSE;
		lpHandler = NULL;
	}
}